package org.sl.flvplayer.data {
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * Receives flashVars from the FLVPlayerApplication class as an object and processes them further </br>
	 * clarifying whether a complete xml playlist has to be loaded or single flashVars.</br> 
	 * It then initializes the Config class and the Playlist class.</br>
	 * Furthermore when a video_src parameter is found a new xml videoNode is createt and parsed to the addVideoSource method of the Playlist class. 
	 * 
	 * @package org.sl.flvplayer.data
	 * @author Dan
	 * @version
	 * @see org.sl.flvplayer.main.FLVPlayerApplication
	 * @see org.sl.flvplayer.data.Config
	 * @see org.sl.flvplayer.data.Playlist
	 */ 
	public class InitData {

		private static var instance : InitData;

		private var playlistXML : XML;
		private var config : Config;
		private var playlist : Playlist;

		/**
		 * Ensures that there is only one instance of the class by using the singleton pattern.
		 *
		 * @return instance of the <code>InitData</code> class 
		 */
		public static function getInstance() : InitData {
			if(!instance) instance = new InitData();
			return instance;
		}

		/**
		 * Checks for the playlist parameter name in the parsed p_params object.
		 * 
		 * @param p_params flashVars 
		 */
		public function init(p_params : Object) : void { 			
			//for testing purposes only
			//	for (var i:String in p_params) 
			//		FLVPlayerApplication.SOS_TRACER.alert(i + " = " + p_params[i]);
			//	}
			
			if(p_params.playlist) {
				//playlist found!
				loadPlaylist(p_params.playlist);
			} else {
				parseParams(p_params);
			}
		}

		/**
		 * Loads the xml playlist if the playlist parametername is found.
		 * 
		 * @param p_url path to the xml playlist
		 */
		private function loadPlaylist(p_url : String) : void {
			var request : URLRequest = new URLRequest(p_url);
			var loader : URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, playlistComplete);
			loader.load(request);
		}

		/**
		 * Creates and passes an xml object from the exsisting xml playlist to the init methods of their respective class. 
		 * 
		 * @param p_event 
		 */
		private function playlistComplete(p_event : Event) : void {
			playlistXML = new XML(URLLoader(p_event.target).data);
			// process the config part
			config = Config.getInstance();
			config.init(playlistXML.config[0]);
			// process the playlist part
			playlist = Playlist.getInstance();
			playlist.init(playlistXML.playlist[0]);				
		}

		/**
		 * Checks for the video_src parameter name and if found one creates an xml object sending it to the addVideoSource method of the Playlist class.</br>
		 * All other parameters are parsed to the setParam method of the Config class.
		 * 
		 * @param p_params flashVars
		 */
		private function parseParams(p_params : Object) : void {
			config = Config.getInstance();	
			playlist = Playlist.getInstance();
			for (var i:String in p_params) {
				if(i == "video_src") {
					// video_src found! create xml object
					var previewImage : String = (p_params.preview_image) ? p_params.preview_image : "0";
					var videoNode : XML = new XML('<video src="' + p_params[i] + '" previewimage= "' + previewImage + '"></video>');
					videoNode.appendChild(new XML('<watermark icon= "' + p_params.watermark_icon + '"  iconpos= "' + p_params.watermark_iconpos + '"></watermark>'));
					videoNode.appendChild(new XML('<link href= "' + p_params.link + '"  icon= "' + p_params.link_icon + '"  iconpos= "' + p_params.link_iconpos + '"></link>'));
					videoNode.appendChild(new XML('<google track= "' + p_params.track + '"></google>'));
					videoNode.appendChild(new XML('<background color= "' + p_params.color + '"></background>'));
					playlist.addVideoSource(videoNode);
				} else {
					// all other params are presumably config params 
					config.setParam(i, p_params[i]);
				}
			}			
		}
	}
}
